package com.gph.saviorframework.base.handler;

import com.gph.saviorframework.util.DateUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * Created by iscas-qd on 2016/3/7.
 * 将时间戳类型转化为yyyy-MM-dd HH:mm:ss格式的字符串
 */
public class DateTypeHandler implements TypeHandler {

    public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
        ps.setDate(i,(java.sql.Date)parameter);
    }

    public Object getResult(ResultSet rs, String columnName) throws SQLException {
        Date date = rs.getTimestamp(columnName);
        return DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss");
    }

    public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
        Date date = rs.getTimestamp(columnIndex);
        return DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss");
    }

    public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
        return null;
    }
}
